package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;

import object.obj2D.graphics.Texture;
import system.Device;
import system.GameOption;
import system.Graphics;
import system.KeyBoard;

import com.game.GamePlay;
import com.game.R;

public class Exit extends State {
	private  final int MENU_ITEM_YES = 0;
	private  final int MENU_ITEM_NO = MENU_ITEM_YES + 1;
	private  final int MENU_ITEM_BEGIN = MENU_ITEM_YES;
	private  final int MENU_ITEM_END = MENU_ITEM_NO;
	private  int sNumMenuItem = 2;

	private Menu	m_menu;
	private int	m_maxOfItem = 2;
	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;
	private int selected;

	private int[] m_iMenuItemTex = new int[sNumMenuItem];
	private int[] m_iMenutemSelectedTex = new int [sNumMenuItem];
	
	private Texture	m_texture = new Texture();
	
	@Override
	public void Init() {
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		selected = MENU_ITEM_BEGIN;
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		float w = m_fSrcWidth/2;
		float h = m_fSrcHeight/2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		//Load menu texture
		//Yes
		m_iMenuItemTex[MENU_ITEM_YES] = m_texture.initTexture(R.drawable.mnu_yes);
		m_iMenutemSelectedTex[MENU_ITEM_YES] = m_texture.initTexture(R.drawable.mnu_yes_select);
		//No
		m_iMenuItemTex[MENU_ITEM_NO] = m_texture.initTexture(R.drawable.mnu_no);
		m_iMenutemSelectedTex[MENU_ITEM_NO] = m_texture.initTexture(R.drawable.mnu_no_select);
		
		m_menu = new Menu(gl, m_maxOfItem, (int) Device.MENU_BUTTON_WIDTH, (int)Device.MENU_BUTTON_HEIGHT);
		
		m_menu.AddMenuItem(m_iMenuItemTex[MENU_ITEM_YES], m_iMenutemSelectedTex[MENU_ITEM_YES]);
		m_menu.AddMenuItem(m_iMenuItemTex[MENU_ITEM_NO], m_iMenutemSelectedTex[MENU_ITEM_NO]);
		m_texture.initTexture(R.drawable.mnu_ext_title);
		m_menu.SetTitle(m_texture.getTextureID(), Size.MNU_EXIT_TITLE_WIDTH, Size.MNU_EXIT_TITLE_HEIGHT);
		m_menu.SetSelectedMenuItem(selected);
		m_texture.initTexture(R.drawable.mainmenu_bg);
		m_menu.SetBackground(m_texture.getTextureID());
	}
	

	@Override
	public void Update(GamePlay gamePlay) {
		super.Update(gamePlay);
		
		// add code here
	}

	@Override
	public void Render() {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		float w = m_fSrcWidth/2;
		float h = m_fSrcHeight/2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glLoadIdentity();

		m_menu.Render();		
	}

	@Override
	public void Destroy() {
		m_menu.Clear();
		m_menu = null;
		m_iMenuItemTex = null;
		m_iMenutemSelectedTex = null;
	}


	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if(KeyBoard.IsKeyPressed(DK_PAD_DOWN))
		{
			selected++;
			if(selected > MENU_ITEM_END)
				selected = MENU_ITEM_END;
			m_menu.SetSelectedMenuItem(selected);
			
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlayChangeMenu();
		}
		else if(KeyBoard.IsKeyPressed(DK_PAD_UP))
		{
			selected--;
			if(selected < MENU_ITEM_BEGIN)
				selected = MENU_ITEM_BEGIN;
			m_menu.SetSelectedMenuItem(selected);
			
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlayChangeMenu();
		}
		else if(KeyBoard.IsKeyPressed(DK_PAD_MIDDLE))
		{
			switch(selected)
			{
			case MENU_ITEM_YES:
				android.os.Process.killProcess(android.os.Process. myPid());
				if (GameOption.isSoundOn)
					GameSound.GetEffSound().PlaySelectMenu();
				break;
			case MENU_ITEM_NO:
				if(!gamePlay.ResumeState())
					gamePlay.SwitchState(GameState.MainMenu);
				
				if (GameOption.isSoundOn)
					GameSound.GetEffSound().PlaySelectMenu();
				break;
			}
		}
	}
}
